//
//  RegisterViewController.m
//  QuickCure
//
//  Created by 马文星 on 16/5/1.
//  Copyright © 2016年 Demos. All rights reserved.
//

#import "RegisterViewController.h"
#import <SMS_SDK/SMSSDK.h>

@interface RegisterViewController ()

/** 手机号 */
@property (weak, nonatomic) IBOutlet UITextField *phoneNumberTextField;

/** 验证码 */
@property (weak, nonatomic) IBOutlet UITextField *verficationTextField;

/** 获取验证码按钮 */
@property (weak, nonatomic) IBOutlet UIButton *verificButton;

/** 小圆点 */
@property (weak, nonatomic) IBOutlet UIButton *agreeProButton;
//下一步
@property (weak, nonatomic) IBOutlet UIButton *nextButton;


@property (nonatomic, strong) NSTimer *timer;

//秒
@property (nonatomic, assign) CGFloat secoends;

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self setupUI];
    
    [self.phoneNumberTextField becomeFirstResponder];

}

-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];

    [self removeTimer];
}



#pragma mark  - 私有方法
- (void)setupUI{

    
    
}


- (void)timeRepeat{
    
    NSString *statuString = nil;
    


    if (_secoends<=0) {
        
        statuString = @"获取验证码";
        self.verificButton.userInteractionEnabled = YES;
        
        [self removeTimer];
        
    }else{
        
        statuString = [NSString stringWithFormat:@"%d秒",(int)_secoends];
        self.verificButton.userInteractionEnabled = NO;
    }
    
    self.verificButton.titleLabel.text = statuString;
    [self.verificButton setTitle:statuString forState:UIControlStateNormal];
    [self.verificButton setTitle:statuString forState:UIControlStateHighlighted];
    
    _secoends -= 1;

}

-(void)removeTimer{
    
    [_timer invalidate];
    _timer = nil;
    
}


#pragma mark  - Actions

//获取验证码
- (IBAction)getVerfication:(id)sender {
    
    if (self.phoneNumberTextField.text.length==0) {
        
        [SVPAlertManager alertErrorStatus:@"请输入您的手机号码"];
        return;
    }

    
    
    [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:self.phoneNumberTextField.text zone:@"86" customIdentifier:nil result:^(NSError *error) {
        
        if (error) {
            
  // error = Error Domain=getVerificationCode Code=477 "(null)" UserInfo={getVerificationCode=当前手机号发送短信的数量超过当天限额
            loggerError(@"error = %@",error);
            
            [SVPAlertManager alertErrorStatus:@"发送验证码失败"];
            
        }else{
            
            _secoends = 60;
            _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeRepeat) userInfo:nil repeats:YES];
            [_timer fire];
            
            NSLog(@"成功");
        }
        
    }];


}


- (IBAction)agressButtonClick:(UIButton *)sender {
    
    sender.selected ^= 1;
    if (sender.selected) {
        
        [self.agreeProButton setImage:[UIImage imageNamed:@"illness_rb_img_sel.png"] forState:UIControlStateSelected];

    }else{
    
        [self.agreeProButton setImage:[UIImage imageNamed:@"illness_rb_img_nor.png"] forState:UIControlStateNormal];
    }
}

//点击下一步
- (IBAction)goNextStep:(UIButton *)sender {
    
    
    if (self.phoneNumberTextField.text.length==0) {
        
        [SVPAlertManager alertErrorStatus:@"请输入您的手机号码"];
        return;
    }
    
    if (self.verficationTextField.text.length==0) {
        
        [SVPAlertManager alertErrorStatus:@"请输入手机号验证码"];
        return;
    }

    
    if (!self.agreeProButton.selected) {
        
        [SVPAlertManager alertErrorStatus:@"请同意并阅读用户协议"];
        return;
    }
    
    if (self.phoneNumberTextField.text.length > 0 ||  self.verficationTextField.text.length > 0){
    
        ///验证 验证码
        [SMSSDK commitVerificationCode:_verficationTextField.text phoneNumber:_phoneNumberTextField.text zone:@"86" result:^(NSError *error) {
            
            if (!error) {
                
                loggerWarn(@"验证成功");
                
                [self login];
                
            }else{
                NSLog(@"错误信息：%@",error);
                
                [SVPAlertManager alertErrorStatus:@"用户注册失败"];
            }
        }];
    
        
    
    }
}


-(void)login{

    WEAKSELF
    [[NetWorkManager sharedManager] loginWithLoadUserInfo:^(UserDTO *dto) {

        //登录成功
        [MLDefaults setBool:YES forKey:kIsUserLoginKey];
        
        //保存数据
        
        [[UserDTO sharedInstance] saveUserInfoToSanbox:dto];
        

        
        //回调处理
        weakSelf.loginBlock();
        
        
        [weakSelf goBack];

        
    }];



}

@end
